<?php
namespace app\order\controller;
use app\order\model\Car;
use app\order\model\City;
use app\order\model\Order;
use app\order\model\Site;
use app\order\model\Userdata;
use think\cache\driver\Redis;
use think\Controller;
use think\Db;

class Index extends Controller {
	public function __construct() {
		parent::__construct();
		if (empty(input('get.tid')) or input('get.tid') == 68) {
			$cate = Db::name('cate')->where('parentid', 68)->select();

			$this->assign('cate', $cate);

		} else {
			$tid = input('get.tid');

			$cate = Db::name('cate')->where('parentid', $tid)->select();

			if (empty($cate)) {
				$cate = Db::name('cate')->where('parentid', 68)->select();
				$this->assign('cate', $cate);

			}
			$this->assign('cate', $cate);

		}
	}
	public function index() {
		return 'asdasdf';
	}
	public function orderConfirm() {
		// 手机号处理
		// 
		if (cookie('user')==null) {
			if(session('user')==null){
				var_dump(cookie('user'));
			header('Location:/index/index/login');
			}
		}
		$carInfo = Car::get(input('get.car_id'));
		// $phone = Userdata::get(['id' => cookie('uid')])->phone;
		// $phone = substr($phone, 0, 3) . '****' . substr($phone, 6, 4);
		$phone = '188'.'****'.'7723';
		$this->assign(['carInfo' => $carInfo, 'phone' => $phone]);

		// var_dump($carInfo->price);

		// redis处理
		$redis = new Redis;

		$info = $redis->get('CUSTOMER_SID_' . md5(cookie('user')));
		// var_dump($info);
		$info['startTime'] = is_string($info['startTime']) ? $info['startTime'] : date('Y/m/d H:i:s', $info['startTime']);
		$info['endTime'] = is_string($info['endTime']) ? $info['endTime'] : date('Y/m/d H:i:s', $info['endTime']);
		$info['fromStoreName'] = Site::where('id', $info['fromStoreName'])->value('name');
		$info['toStoreName'] = Site::where('id', $info['toStoreName'])->value('name');

		// 总价
		$totalPrice = $info['time'] * $carInfo->price + 40 + 20;
		$info['totalPrice'] = $totalPrice;

		$info['car_id'] = input('get.car_id');

		$redis->set('CUSTOMER_SID_' . md5(cookie('user')), $info);
		$this->assign('info', $info);

		// 处理城市信息
		// var_dump($info);
		return view();
	}
	public function car_list() {

		// var_dump(input('post.'));
		// 第一次提交的时候
		if (!input('post.') || strtotime(input('post.pickUpTime')) > strtotime(input('post.returnTime'))) {
			$time = 1;
			$startTime = time();
			$endTime = time() + 3600 * 10;
			$result = Car::where(['site' => 1])->select();
		} else {

			$result = Car::where(['site' => input('post.fta')])->select();
			$startTime = strtotime(input('post.pickUpTime') . ' ' . input('post.pickUpTime_h') . ':00:00');
			$endTime = strtotime(input('post.returnTime') . ' ' . input('post.returnTime_h') . ':00:00');
			$time = ceil(($endTime - $startTime) / 86400);
			if ($time == 0) {
				$time = 1;
			} elseif ($time < 0) {
			}
		}

		$this->assign(['car_list' => $result, 'time' => $time]);

		// 存入redis
		$redis = new Redis;
		$uid = 'CUSTOMER_SID_' . md5(cookie('user'));
		// json 信息
		if (!input('post.')) {
			$info = json_encode(['fromStoreName' => 1, 'startTime' => $startTime, 'endTime' => $endTime, 'toStoreName' => 1, 'fromCity' => 'SH', 'toCity' => 'SH', 'time' => $time]);
		} else {
			$info = json_encode(['fromStoreName' => input('post.fta'), 'startTime' => $startTime, 'endTime' => $endTime, 'toStoreName' => input('post.tca'), 'fromCity' => input('post.fc'), 'toCity' => input('post.tc'), 'time' => $time]);
		}

		$redis->set($uid, $info);
		$a = $redis->get($uid);

		//出发时间
		$arr = ['st' => date('Y/m/d', $a['startTime']), 'rt' => date('Y/m/d', $a['endTime'])];
		$this->assign('redis', $arr);

		// $this->assign(['totalPrice'=>$totalPrice]);
		return view();
	}

	public function payment() {
		$redis = new Redis;
		$uid = 'CUSTOMER_SID_' . md5(cookie('user'));
		$info = $redis->get($uid);
		$this->assign('info', $info);
		return view();
	}

	public function booksuccess() {
		// 插入数据
		$insert = [];
		// 下订单
		if (session('uid')) {
			// var_dump(session('uid'));
			$insert['user_id'] = session('uid');
		}else{
			// var_dump(cookie('uid'));
			$insert['user_id'] = cookie('uid');
		}
		
		// 订单号 order_id
		$insert['order_id'] = date('ymdHis', time()) . cookie('uid');

		$redis = new Redis();
		$info = $redis->get('CUSTOMER_SID_' . md5(cookie('user')));
		$insert['borrow_city'] = $info['fromCity'];
		$insert['return_city'] = $info['toCity'];
		$insert['fee'] = $info['totalPrice'];
		$insert['borrow_time'] = strtotime($info['startTime']);
		$insert['return_time'] = strtotime($info['endTime']);
		$insert['car_id'] = $info['car_id'];
		// 0预约 1完成 -1 取消
		$insert['status'] = 0;
		$newOrder = new Order($insert);
		// 若提交过订单则不让他下单
		if ($a = Order::where(['borrow_city' => $insert['borrow_city'], 'return_city' => $insert['return_city'], 'borrow_time' => $insert['borrow_time'], 'return_time' => $insert['return_time']])->find()) {
			$id = $a->order_id;
		} else {
			$newOrder->save();
			$id = $newOrder->order_id;
		}

		// 页面信息
		// var_dump($info);
		$info['fromStoreName'] .= '店';
		$info['toStoreName'] .= '店';
		$info['startTime'] = date('y' . '年' . 'm' . '月' . 'd' . '日' . 'H' . '点' . 'i' . '分', $insert['borrow_time']);
		$info['endTime'] = date('y' . '年' . 'm' . '月' . 'd' . '日' . 'H' . '点' . 'i' . '分', $insert['return_time']);
		$info['fromCity'] = City::where(['name' => $info['fromCity']])->value('cname');
		$info['toCity'] = City::where(['name' => $info['toCity']])->value('cname');
		$info['order_id'] = $id;
		// var_dump($info);
		$this->assign('info', $info);

		// 车辆信息
		$carInfo = Car::get($info['car_id']);
		$carInfo->desc = str_replace('|', '/', $carInfo->desc);
		// var_dump($carInfo);
		$this->assign('carInfo', $carInfo);

		return view();
	}
	/**
	 * 取消订单
	 * @return [type] [description]
	 */
	public function cancel() {

		return view();
	}

}
